<?php
class PageIndex {
	var $startindex;
	var $endindex;

	function __construct($startindex,$endindex){
		$this->startindex = $startindex;
		$this->endindex = $endindex;
	}

	public static function getPageIndex($viewpagecount, $currentPage, $totalpage){
		$startpage = $currentPage-($viewpagecount%2==0? intval($viewpagecount/2)-1 : intval($viewpagecount/2));
		$endpage = $currentPage+intval($viewpagecount/2);
		if($startpage<1){
			$startpage = 1;
			if($totalpage>=$viewpagecount) $endpage = $viewpagecount;
			else $endpage = $totalpage;
		}
		if($endpage>$totalpage){
			$endpage = $totalpage;
			if(($endpage-$viewpagecount)>0) $startpage = $endpage-$viewpagecount+1;
			else $startpage = 1;
		} 
		return new PageIndex($startpage, $endpage);
	}
}

?>